/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package commonInfrastructure.accountManagement.session;

import javax.ejb.LocalBean;
import commonInfrastructure.accountManagement.entity.EmployeeEntity;
import commonInfrastructure.accountManagement.entity.SalesmanEntity;
import commonInfrastructure.system.session.AuditTrailSessionBean;
import commonInfrastructure.system.session.SystemSessionBean;
import java.text.ParseException;
import java.util.List;
import javax.ejb.EJB;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.NoResultException;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

/**
 *
 * @author Wei Haoyang
 */
@Stateless
@LocalBean
public class EmployeeSessionBean {

    // Add business logic below. (Right-click in editor and choose
    // "Insert Code > Add Business Method")
    @PersistenceContext
    private EntityManager em;
    private EmployeeEntity employee;
    
    @EJB
    SystemSessionBean systemSession = new SystemSessionBean();
    @EJB
    AuditTrailSessionBean auditSession = new AuditTrailSessionBean();
    
    public EmployeeEntity getEmployeeById(Long EmployeeId)
    {
        return em.find(EmployeeEntity.class, EmployeeId);
    }
    
    public void resetEmployee(EmployeeEntity employeeTemp ){
        
        employee=employeeTemp;
        em.merge(employee);
        
    }
    
    public SalesmanEntity getSalesmanById(Long salesmanId)
    {
        SalesmanEntity salesman = null;
        try{
            salesman = em.find(SalesmanEntity.class, salesmanId);
        }
        catch(NoResultException ex){
            ex.printStackTrace();
        }
        return salesman;
    }
    
    
    public List<EmployeeEntity> getEmployeeByName(String userName) throws NoResultException
    {
        Query query = em.createQuery("SELECT u FROM EmployeeEntity u WHERE u.name = :inUserName");
        query.setParameter("inUserName", userName);
        List<EmployeeEntity> employee = null;
        employee = query.getResultList();

        return employee;
    }
    
    public EmployeeEntity getEmployeeByNameSingle(String userName) throws NoResultException
    {
        Query query = em.createQuery("SELECT u FROM EmployeeEntity u WHERE u.name = :inUserName");
        query.setParameter("inUserName", userName);
        List<EmployeeEntity> employee = null;
        employee = query.getResultList();

        return employee.get(0);
    }
    
    public boolean isExist(EmployeeEntity employee){
        Query query = em.createQuery("SELECT u FROM EmployeeEntity u WHERE u.userName = :inUserName");
        query.setParameter("inUserName", employee.getUserName());
        EmployeeEntity employeeTemp = null;
        try{
        employeeTemp = (EmployeeEntity)query.getSingleResult();
        return true;//if there is such username
        }catch(NoResultException ex){
            return false;
        }
    }
    
    public void addEmployee (EmployeeEntity employee) throws ParseException 
    {   
        em.persist(employee);
        em.flush();
        //Audit Trail
//        EmployeeEntity user = systemSession.getCurrentUser();
//        auditSession.addAuditTrail(systemSession.getToday(), employee.getId(), user.getUserName(), "commonInfrastructure.accountManagement", "Employee", "Add", "");
    }
                 
             
       
    public void editEmployee(EmployeeEntity employee) throws ParseException
    {
        em.merge(employee);
        //Audit Trail
        EmployeeEntity user = systemSession.getCurrentUser();
        auditSession.addAuditTrail(systemSession.getToday(), employee.getId(), user.getUserName(), "commonInfrastructure.accountManagement", "Employee", "Edit", "");
    }
    

    public void removeEmployee(Long EmployeeId) throws ParseException
    {
        EmployeeEntity employee = em.find(EmployeeEntity.class, EmployeeId);
        em.remove(employee);
        
        //Audit Trail
        EmployeeEntity user = systemSession.getCurrentUser();
        auditSession.addAuditTrail(systemSession.getToday(), EmployeeId, user.getUserName(), "commonInfrastructure.accountManagement", "Employee", "Delete", "");
    }
    
    public List<EmployeeEntity> getAllEmployees() throws NoResultException{
        Query query = em.createQuery("SELECT s1 FROM EmployeeEntity s1");
        return query.getResultList();
    }
    
    public EmployeeEntity getEmployeeByUserName(String uName){
        Query query = em.createQuery("SELECT u FROM EmployeeEntity u WHERE u.userName = :inUserName");
        query.setParameter("inUserName", uName);
        EmployeeEntity employee = null;
        try{
            employee = (EmployeeEntity)query.getSingleResult();
        }
        catch(NoResultException ex){
            throw ex;
        }
        return employee;
    }

    public EmployeeEntity getEmployee() {
        return employee;
    }

    public void setEmployee(EmployeeEntity employee) {
        this.employee = employee;
    }
   

}
